<template>
  <div class="wallet-page">
    <!-- 头部导航 -->
    <el-header class="page-header">
      <div class="back-btn" @click="$router.back()">
        <i class="el-icon-arrow-left"></i>
      </div>
      <h1>我的优惠券</h1>
      <div class="empty-btn"></div>
    </el-header>

    <div class="points-section">
      <div class="points-header">
        <h2 class="points-title">我的积分</h2>
      </div>
      <div class="points-card">
        <div class="points-value">
          <span class="points-num">{{ userPointInfo.points || 0 }}</span>
          <span class="points-unit">积分</span>
        </div>
        <el-button type="primary" class="exchange-btn" :disabled="false" @click="$router.push(`/coupon/${userPointInfo.points}`)">
          去兑换
        </el-button>
      </div>
    </div>

    <!-- 我的优惠券区域：完全绑定表字段 -->
    <div class="coupon-section">
      <div class="coupon-header">
        <h2 class="coupon-title">我的优惠券</h2>
      </div>

      <div class="coupon-list">
        <!-- 动态渲染优惠券：绑定表中所有字段 -->
        <div
            class="coupon-card"
            v-for="coupon in couponList"
            :key="coupon.id"
            :class="[
            coupon.status === 2 ? 'used-coupon' : '',  // 已使用（status=2）
            coupon.status === 3 ? 'expired-coupon' : '' // 已过期（status=3）
          ]"
            v-if="
            // 按表中status字段筛选：1=未使用、2=已使用、3=已过期
            (activeTab === 'all') ||
            (activeTab === 'unused' && coupon.status === 1) ||
            (activeTab === 'used' && coupon.status === 2) ||
            (activeTab === 'expired' && coupon.status === 3)
          "
        >
          <!-- 优惠券左侧：显示表中name/discount/expireTime字段 -->
          <div class="coupon-left">
            <!-- 表中name字段：优惠券名称 -->
            <div class="coupon-name">{{ coupon.name || '无名称优惠券' }}</div>

            <!-- 表中discount字段：折扣（改为“X折”显示） -->
            <div class="coupon-discount">
              <span class="discount-value">{{ coupon.discount }}折</span>
            </div>

            <!-- 表中expireTime字段：有效期 -->
            <div class="coupon-date">
              有效期至：{{coupon.expireTime}}
              <!-- 表中getTime字段：领取时间 -->
              <span class="get-time">（{{coupon.getTime}}领取）</span>
            </div>
          </div>

          <!-- 优惠券右侧：按表中status字段显示对应状态 -->
          <div class="coupon-right">
            <el-tag type="info" v-if="coupon.status === 1">未使用</el-tag>
            <div v-else-if="coupon.status === 2" class="used-info">
              <el-tag type="success">已使用</el-tag>
              <div class="use-time">使用时间：{{coupon.useTime}}</div>
            </div>
            <el-tag type="info" v-else-if="coupon.status === 3">已过期</el-tag>
          </div>
        </div>

        <!-- 空状态：无数据时显示 -->
        <div class="empty-state" v-if="
          couponList.length === 0 ||
          (activeTab === 'unused' && !couponList.some(c => c.status === 1)) ||
          (activeTab === 'used' && !couponList.some(c => c.status === 2)) ||
          (activeTab === 'expired' && !couponList.some(c => c.status === 3))
        ">
          <el-empty description="暂无对应状态的优惠券，请去兑换吧"></el-empty>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { ArrowLeft } from '@element-plus/icons-vue'
import { queryByUserId } from "../api/wallet.js";
import {queryAll, queryByUserIdCoupon} from "../api/userCoupon.js";

export default {
  name: "Wallet",
  components: {
    ArrowLeft
  },
  data() {
    return {
      userPointInfo: {
        id: null,
        userId: null,
        points: 0,
        couponId: null,
        groupBuyId: null
      },
      couponList: [],  // 存储我的优惠券的表数据
      activeTab: 'all',
    }
  },
  methods: {
    async loadData() {
      let { data: data } = await queryByUserId()
      console.log(data)
      this.userPointInfo = data.data
    },
    async loadData2() {
      let { data: data } = await queryByUserIdCoupon()
      console.log(data)  // 可查看表字段是否匹配
      this.couponList =  data.data
    }
  },
  mounted() {
    this.loadData()
    this.loadData2()
  }
}
</script>

<style scoped>
/* 全局样式 */
.wallet-page {
  width: 100vw;
  min-height: 100vh;
  background-color: #f5f7fa;
  font-size: 14px;
  color: #333;
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

/* 头部导航 */
.page-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 16px;
  background-color: #409EFF;
  color: #fff;
  height: 60px;
  box-sizing: border-box;
}

.back-btn {
  width: 32px;
  height: 32px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 50%;
  background-color: rgba(255, 255, 255, 0.2);
  cursor: pointer;
}

.back-btn .el-icon-arrow-left {
  font-size: 18px;
}

.page-header h1 {
  font-size: 18px;
  font-weight: 600;
  margin: 0;
}

.empty-btn {
  width: 32px;
  height: 32px;
}

/* 积分区域 */
.points-section {
  padding: 16px;
  background-color: #fff;
  margin: 16px;
  border-radius: 8px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.05);
}

.points-header {
  margin-bottom: 16px;
}

.points-title {
  font-size: 16px;
  font-weight: 600;
  margin: 0;
}

.points-card {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 20px 16px;
  background: linear-gradient(135deg, #409EFF, #67C23A);
  border-radius: 8px;
  color: #fff;
}

.points-value {
  display: flex;
  align-items: baseline;
}

.points-num {
  font-size: 32px;
  font-weight: 700;
  margin-right: 8px;
}

.points-unit {
  font-size: 16px;
}

.exchange-btn {
  padding: 8px 16px;
  border-radius: 20px;
  background-color: #fff;
  color: #409EFF;
  border: none;
}

.exchange-btn:disabled {
  background-color: rgba(255, 255, 255, 0.6);
  color: #888;
  cursor: not-allowed;
}

/* 优惠券区域：匹配表字段显示样式 */
.coupon-section {
  padding: 0 16px 24px;
  box-sizing: border-box;
}

.coupon-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 16px;
  flex-wrap: wrap;
  gap: 8px;
}

.coupon-title {
  font-size: 16px;
  font-weight: 600;
  margin: 0;
}

.coupon-tabs {
  width: auto;
  flex-shrink: 0;
}

.el-tabs__nav {
  white-space: nowrap;
}

.el-tabs__item {
  font-size: 13px;
  padding: 0 12px;
}

/* 优惠券列表容器 */
.coupon-list {
  background-color: #fff;
  border-radius: 8px;
  overflow: hidden;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.05);
  margin: 0;
  padding: 0;
}

/* 优惠券卡片：适配表字段显示布局 */
.coupon-card {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 16px;
  border-bottom: 1px solid #f0f0f0;
  box-sizing: border-box;
  width: 100%;
  background-color: #fff;
}

.coupon-card:last-child {
  border-bottom: none;
}

/* 已使用优惠券样式：灰色调 */
.used-coupon {
  background-color: #fafafa;
}
.used-coupon .coupon-name,
.used-coupon .discount-value,
.used-coupon .coupon-date,
.used-coupon .get-time {
  color: #999 !important;
}

/* 已过期优惠券样式：灰色调+斜纹 */
.expired-coupon {
  background-color: #fafafa;
  position: relative;
}
.expired-coupon::after {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-image: linear-gradient(45deg, #eee 25%, transparent 25%, transparent 75%, #eee 75%, #eee),
  linear-gradient(45deg, #eee 25%, transparent 25%, transparent 75%, #eee 75%, #eee);
  background-size: 6px 6px;
  opacity: 0.3;
  pointer-events: none;
}
.expired-coupon .coupon-name,
.expired-coupon .discount-value,
.expired-coupon .coupon-date,
.expired-coupon .get-time {
  color: #999 !important;
}

/* 优惠券左侧字段显示 */
.coupon-left {
  flex: 1;
  padding-right: 16px;
  box-sizing: border-box;
}

/* 表中name字段样式 */
.coupon-name {
  font-size: 16px;
  font-weight: 500;
  color: #333;
  margin-bottom: 8px;
}

/* 表中discount字段样式（折扣） */
.coupon-discount {
  margin-bottom: 8px;
}

.discount-value {
  font-size: 28px;
  font-weight: 700;
  color: #FF4D4F;
}

/* 表中expireTime/getTime字段样式 */
.coupon-date {
  font-size: 12px;
  color: #666;
}

.get-time {
  font-size: 11px;
  color: #999;
  margin-left: 8px;
}

/* 优惠券右侧状态显示 */
.coupon-right {
  width: 100px;
  text-align: right;
  flex-shrink: 0;
}

.use-btn {
  color: #409EFF;
  font-size: 14px;
  padding: 0;
}

/* 已使用状态的使用时间样式 */
.used-info {
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  gap: 4px;
}

.use-time {
  font-size: 11px;
  color: #999;
}

/* 已使用/已过期标签样式 */
.el-tag {
  margin-top: 4px;
}

/* 空状态样式 */
.empty-state {
  padding: 60px 20px;
  text-align: center;
  color: #999;
  background-color: #fff;
}

/* 移动端适配 */
@media (max-width: 375px) {
  .coupon-header {
    flex-direction: column;
    align-items: flex-start;
    gap: 8px;
  }

  .coupon-tabs {
    width: 100%;
  }

  .coupon-card {
    flex-direction: column;
    align-items: flex-start;
    gap: 12px;
  }

  .coupon-right {
    width: 100%;
    text-align: left;
  }

  .used-info {
    align-items: flex-start;
  }
}
</style>